# -*- coding: utf-8 -*-
import unittest, os, os.path, re, time, multiprocessing
from playdoh03 import *

class BaseLocalhostTest(unittest.TestCase):
    def setUp(self):
        log.debug("STARTING SERVER")
        self.p = multiprocessing.Process(target=open_base_server)
        self.p.start()
        time.sleep(.1)
    
    def tearDown(self):
        log.debug("CLOSING SERVER")
        close_base_servers('localhost')
        self.p.join(3.0)
        time.sleep(.2)

class LocalhostTest(unittest.TestCase):
    def setUp(self):
        log.debug("STARTING SERVER")
        self.p = multiprocessing.Process(target=open_server)
        self.p.start()
        time.sleep(.1)
    
    def tearDown(self):
        log.debug("CLOSING SERVER")
        close_servers('localhost')
        self.p.join(3.0)
        time.sleep(.2)

        
        
        
def all_tests():
    folder = os.path.dirname(os.path.realpath(__file__)) # TODO: change that : put tests in test/ dir 
    pattern = '^(test_[^.]+).py$'
    files = os.listdir(folder)
    files = [file for file in files if re.match(pattern, file)]
    
    suites = []
    for file in files:
        modulename = re.sub(pattern, '\\1', file)
        module = __import__(modulename)
        try:
            suites.append(module.test_suite())
        except:
            log.warn("module '%s' has no method 'test_suite'" % modulename)
    
    allsuites = unittest.TestSuite(suites)
    return allsuites

if __name__ == '__main__':
    unittest.main(defaultTest='all_tests')
